#ifndef	__DICT_H__
#define __DICT_H__

#include <map>
#include <string>
#include "port.h"

class CDict {
public:
  struct TState;
  typedef const TState *PState;
  struct TState {
    TWORDID word_id;
    std::map<TChar, TState> *follow;
    TState(TWORDID wid = ID_NOT_WORD) : word_id(wid), follow(NULL) {}
  };
  typedef std::map<TChar, TState> Map_Type;
  CDict() : m_root() {}
  ~CDict() { close(); }

  bool parseText(const char *filename);
  void close() {
    freeSubTree(m_root);
    m_root = TState();
  }
    
  const TState* getRoot() const {
    return &m_root;
  }
    
  int matchLongest(const CDict::TState *root, CDict::PState &result,
                   const TWCHAR *str);
  static const TState* step(const CDict::TState *root, TWCHAR wch);
								
  void PrintOut(FILE *fp) {
    wstring ws;
    InnerPrint(fp, ws, getRoot());
  }
    
protected:
  TState m_root;
  void freeSubTree(TState& root);
  void insertWord(const TWCHAR *wstr, TWORDID id);
  void InnerPrint(FILE *fp, wstring &wstr, const TState *pnode);
};

#endif
  
    
